<?php
class Bootstrap {
	
	private static $instance = null;
	
	private function __construct() {}
	
	public function boot() {
		require_once BASE_PATH . 'etc/config.php';
		require_once APP_PATH . 'etc/config.php';
		
		define('DEBUG_ENABLED', $config['debug']);
		
		if (isset($config['classpath'])) {
			$this->setupClassPath($config['classpath']);
		}
		
		require_once BASE_PATH . 'etc/core.php';
		
		require_once APP_PATH . 'etc/acl.php';
		SecurityContext::instance()->setup($acl);
		
		AppContext::setup($config['app']);
		
		if (!ini_get('session.auto_start')) {
			Zend_Session::start();
		}
		
		ZLib_DAL_Manager::setup($config['db']);
		
		Zend_Layout::startMvc($config['mvc']['view']);
		
		RedirectHandler::instance()->enableDebug(DEBUG_ENABLED);
		
		$frontController = Zend_Controller_Front::getInstance();
		$frontController->setControllerDirectory($config['mvc']['controllersPath']);
		$frontController->throwExceptions(DEBUG_ENABLED);
		$frontController->dispatch();
	}
	
	private function setupClassPath($classpaths) {
		$classpath = ini_get('include_path');
		
		foreach ($classpaths as $cp) {
			$classpath.= PATH_SEPARATOR . $cp;
		}
		
		ini_set('include_path', $classpath);
	}
	
	public static function instance() {
		if (self::$instance == null) {
			self::$instance = new Bootstrap();
		}
		
		return self::$instance;
	}
}